package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.vo.ContractProductVo;
import cn.itcast.vo.ExportProductVo;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cargo/packing")
public class PackingController extends BaseController {


    @Reference
    private PackingService packingService;

    @Reference
    private ExportService exportService;

    @Reference
    private ExportProductService exportProductService;

    //点击装箱管理,跳转到装箱管理单页面
    @RequestMapping("list")
    public String list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "5") int size) {
        PackingExample example = new PackingExample();
        PageInfo list = packingService.findAll(page, size, example);
        request.setAttribute("page", list);
        return "cargo/packing/packing-list";
    }

    //新建装箱单
    @RequestMapping("toAdd")
    public String toAdd() {
        //下方需要展示已被海关审核通过的报运单信息
        ExportExample example = new ExportExample();
        ExportExample.Criteria criteria = example.createCriteria();
        criteria.andStateEqualTo(2L);
        List<Export> list = exportService.findAll(example);
        request.setAttribute("page", list);
        return "cargo/packing/packing-add";
    }

    //保存新建的装箱单
    @RequestMapping("edit")
    public String edit(Packing packing, String id) {
        packing.setCompanyId(getLoginCompanyId());
        packing.setCompanyName(getLoginCompanyName());
        Integer save = packingService.save(packing, id);
        if (save==0){
            return "/cargo/packing/error";
        }
        return "redirect:/cargo/packing/list.do";
    }

    //删除
    @RequestMapping("delete")
    public String delete(String id) throws IOException { //装箱单id
        //根据装箱单id查询出该装箱单的状态
        Packing packing = packingService.findById(id);
        //只能删除装箱单状态为0的
        if (packing.getState().equals(0)) {
            packingService.delete(id);
        }

        return "redirect:/cargo/packing/list.do";
    }


    //下载Excel
    @ResponseBody
    @RequestMapping("printExcel")
    public void printExcel(String id) throws IOException {//装箱id

        //查询数据 查出报运单id集合
        Packing packing = packingService.findById(id);
        String exportId = packing.getExportIds();//报运单id
        String[] exportIds = exportId.split(",");//报运单id 数组
        List<ExportProductVo> list = new ArrayList();
        for (String eid : exportIds) {
            //报运单id
            System.out.println(eid);
            List<ExportProductVo> epv = exportProductService.findByEid(eid);
            list.addAll(epv);
        }
        Map map = new HashMap();
        map.put("buyer", packing.getBuyer());
        map.put("seller", packing.getSeller());

        //2、加载数据模板
        String path = session.getServletContext().getRealPath("/") + "/make/packing.xlsx";
        //3、设置EasyExcel的文件下载头信息
        response.setContentType("application/vnd.ms-excel"); //下载excel
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("装箱表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        //4、调用工具类创建Excel对象
        ExcelWriter build = EasyExcel.write(response.getOutputStream()) //response的输出输出流
                .head(ExportProductVo.class) //设置表头的对象字节码
                .withTemplate(path)
                .build();
        //5、获取页对象
        WriteSheet sheet = EasyExcel.writerSheet().build();
        //6、填充map数据
        build.fill(map, sheet);
        //7、填充list数据
        build.fill(list, sheet);
        //8.属性资源，完成下载
        build.finish(); //下载excel文件，释放内存资源
    }
}
